package cn.houserent.dao;

import cn.houserent.pojo.Users;
import org.hibernate.Query;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

public class UsersDao extends BaseDao {

    public Users get(Serializable id) {
        // 通过Session的get( )方法根据ID加载指定对象
        return (Users)HibernateUtil.currentSession().get(Users.class,id);
    }

    public Users load(Serializable id) {
        //通过Session的load()方法根据ID加载指定对象
        return (Users)HibernateUtil.currentSession().load(Users.class,id);
    }

    public Users merge(Users users) {
        return (Users)HibernateUtil.currentSession().merge(users);
    }

    public void saveOrUpdate(Users users) {
        HibernateUtil.currentSession().saveOrUpdate(users);
    }

    public void userSave(Users users) {
        currentSession().save(users);
    }

    public void userDelete(Users users) {
        currentSession().delete(users);
    }

    public List<Users> findAllUsers() {
        String hql = "from Users";      //定义HQL语句
        Query query = currentSession().createQuery(hql);    //构建Query对象
        return query.list();
    }

    public Iterator<Users> findUsersByName(String name) {
        String hql = "from Users where name = '"+name+"'";
        Query query = currentSession().createQuery(hql);
        return query.iterate();
    }

    public List<Users> findHousesByPage(int pageNo, int pageSize) {
        String hql = "select new Users(name, telephone) from Users order by id";
        return this.currentSession().createQuery(hql)
                .setFirstResult((pageNo - 1) * pageSize)
                .setMaxResults(pageSize).list();
    }


    /**
     *  用户登录
     * @param name
     * @param password
     * @return
     */
    public List<Users> loginUser(String name,String password) {
        String hql = "from Users where name=:name and password=:password";
        Query query = currentSession().createQuery(hql);
        query.setString("name",name);
        query.setString("password",password);
        return query.list ();
    }


}
